/*******************************************************************************
 * Copyright (c) 2006 CEA List.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     CEA List - initial API and implementation
 *******************************************************************************/
package com.cea.papyrus.core.helper;


import org.eclipse.draw2d.geometry.Dimension;
import org.eclipse.draw2d.geometry.Point;
import org.eclipse.ui.PlatformUI;
import org.eclipse.uml2.uml.Association;
import org.eclipse.uml2.uml.Element;
import org.eclipse.uml2.uml.InstanceSpecification;
import org.eclipse.uml2.uml.Package;
import org.eclipse.uml2.uml.PackageableElement;
import org.eclipse.uml2.uml.UMLFactory;

import com.cea.papyrus.core.ui.dialogs.ChooseLinkAssociationDialog;
import com.cea.papyrus.core.utils.Debug;
import com.cea.papyrus.diagraminterchange2.di2.Diagram;
import com.cea.papyrus.diagraminterchange2.di2.GraphElement;
import com.cea.papyrus.diagraminterchange2.di2.Uml1SemanticModelBridge;

// TODO: Auto-generated Javadoc
/**
 * The Class EdgeInstanceSpecificationFactory.
 */
public class EdgeInstanceSpecificationHelper extends GraphEdgeHelper implements IGraphEdgeHelper {
	
	
	/*
	 * (non-Javadoc)
	 * @see com.cea.papyrus.core.common.factories.GraphEdgeFactory#canExecute(com.cea.papyrus.diagraminterchange2.di2.GraphNode)
	 */
	/**
	 * Can create graph element.
	 * 
	 * @param source the source
	 * 
	 * @return true, if can create graph element
	 */
	@Override
	public boolean canCreateGraphElement(GraphElement source){
		Element eltSource=null;
		if ((source instanceof Diagram)  ){
			if (((Uml1SemanticModelBridge)((Diagram)source).getOwner()).getElement()!=null){
				eltSource=((Uml1SemanticModelBridge)((Diagram)source).getOwner()).getElement();
				if (eltSource instanceof Package){return true;}
			}
		}
		if (source.getSemanticModel()!=null ){
			if (((Uml1SemanticModelBridge)source.getSemanticModel()).getElement()!=null){
				eltSource=((Uml1SemanticModelBridge)source.getSemanticModel()).getElement();
				if (eltSource instanceof  Package){// they are  classifiers
					return true;
				}
			}
		}
		return false;
	}
	/*
	 * (non-Javadoc)
	 * @see com.cea.papyrus.core.common.factories.GraphEdgeFactory#createGraphEdge(org.eclipse.uml2.uml.Element, com.cea.papyrus.diagraminterchange2.di2.Diagram)
	 */
//	@Override
//	public ArrayList createGraphEdge(Element umlElement, Diagram diagram){
//		Debug.debug(this,"createGraphEdge", 10);
//		GraphEdge ge=(GraphEdge)this.createGraphElement(umlElement, null, null, null);
//		//look for UML connected UML element
//		InstanceSpecification is=(InstanceSpecification)umlElement;
//
//		Element umlsupplier=(Element)is.getSlots().get(0);
//		Element umlsource=(Element)is.getSlots().get(0);
//		super.endCreateGraphElement(umlsource, umlsupplier, ge, diagram);
//		return new ArrayList();
//	}
	/*
	 * (non-Javadoc)
	 * @see com.cea.papyrus.core.common.factories.NodeFactory#createGraphNodeWithSemantic(org.eclipse.uml2.uml.Element)
	 */
	/**
	 * Creates the graph element.
	 * 
	 * @param umlChild the uml child
	 * @param size the size
	 * @param location the location
	 * @param parent the parent
	 * 
	 * @return the graph element
	 */
	@Override
	public GraphElement createGraphElement(Element umlChild, GraphElement parent, Point location, Dimension size) {

		GraphElement graphElement=super.createGraphElement(umlChild, null, location, size);
		NodeHelper nf=new NodeHelper();
//		name of the dependency
		GraphElement graphNode=nf.createGraphElement(null, graphElement, new Point(0, 10), null);
		graphNode.setFontSize(9);
		graphNode.setIsVisible(false);

//		stereotype of dependency
		graphNode=nf.createGraphElement(null, graphElement, new Point(0, 30), null);
		graphNode.setFontSize(9);

		//the parent has to be connected in last, because an event will be triggered.
		if(parent!=null){
			parent.getContained().add(graphElement);
		}

		return graphElement;	
	}

	/////////////////////////////////////////////////////////////////////////////////
	//                       UMLManagement
	/////////////////////////////////////////////////////////////////////////////////
	/* (non-Javadoc)
	 * @see com.cea.papyrus.core.common.factories.GraphEdgeFactory#createUMLElement(java.lang.String, org.eclipse.uml2.uml.Element)
	 */
	/**
	 * Creates the UML element.
	 * 
	 * @param umlParent the uml parent
	 * @param name the name
	 * 
	 * @return the element
	 */
	@Override
	public  org.eclipse.uml2.uml.Element createUMLElement(String name, org.eclipse.uml2.uml.Element umlParent){
		Debug.debug(this,"create link", channel); //$NON-NLS-1$
		InstanceSpecification is= UMLFactory.eINSTANCE.createInstanceSpecification();
		is.setName(name);
		
		(umlParent.getNearestPackage()).getPackagedElements().add(is);
		return is;		
	}


	/* (non-Javadoc)
	 * @see com.cea.papyrus.core.common.factories.GraphEdgeFactory#canCreateUMLElement(org.eclipse.uml2.uml.Element)
	 */
	/**
	 * Can create UML element.
	 * 
	 * @param umlParent the uml parent
	 * 
	 * @return true, if can create UML element
	 */
	@Override
	public boolean canCreateUMLElement(Element umlParent) {
		if(umlParent instanceof Package) {
			return true;
		}
		return false;
	}

	/* (non-Javadoc)
	 * @see com.cea.papyrus.core.common.factories.GraphEdgeFactory#addUMLElement(org.eclipse.uml2.uml.Element, org.eclipse.uml2.uml.Element)
	 */
	/**
	 * Adds the UML element.
	 * 
	 * @param element the element
	 * @param umlParent the uml parent
	 * 
	 * @return true, if add UML element
	 */
	@Override
	public boolean addUMLElement(Element element, Element umlParent) {
		if(umlParent instanceof Package) {
			return ((Package)umlParent).getPackagedElements().add((PackageableElement)element);
		}
		return false;
	}

	/* (non-Javadoc)
	 * @see com.cea.papyrus.core.common.factories.GraphEdgeFactory#removeUMLElement(org.eclipse.uml2.uml.Element, org.eclipse.uml2.uml.Element)
	 */
	/**
	 * Removes the UML element.
	 * 
	 * @param element the element
	 * @param umlParent the uml parent
	 * 
	 * @return true, if remove UML element
	 */
	@Override
	public boolean removeUMLElement(Element element, Element umlParent) {
		if(umlParent instanceof Package) {
			return ((Package)umlParent).getPackagedElements().remove(element);
		}
		return false;
	}

	/* (non-Javadoc)
	 * @see com.cea.papyrus.core.common.factories.GraphEdgeFactory#getBaseString()
	 */
	/**
	 * Gets the base string.
	 * 
	 * @return the base string
	 */
	@Override
	public String getBaseString() {
		return "InstanceSpecification"; //$NON-NLS-1$
	}
	
	/////////////////////////////////////////////////////////////////////////////////
	//                       LINK MANAGEMENT
	/////////////////////////////////////////////////////////////////////////////////
	
	/* (non-Javadoc)
	 * @see com.cea.papyrus.core.common.factories.GraphEdgeFactory#connect(org.eclipse.uml2.uml.Element, org.eclipse.uml2.uml.Element, org.eclipse.uml2.uml.Element)
	 */
	/**
	 * Connect.
	 * 
	 * @param target the target
	 * @param link the link
	 * @param source the source
	 * 
	 * @return true, if connect
	 */
	@Override
	public boolean connect(Element source, Element target, Element link) {
		InstanceSpecification instanceSpec=(InstanceSpecification)link;
		instanceSpec.setName("Link");
		
		/*
		 * Determine the right association to classify this instance specification link. 
		 * There are cases where more than one candidate could possible ==> the user has to choose.
		 * 
		 */
		Association assoc = null; 
		ChooseLinkAssociationDialog dialog = new ChooseLinkAssociationDialog(PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell(), instanceSpec, (InstanceSpecification)source, (InstanceSpecification)target);
		int val = dialog.open();
		if(val == ChooseLinkAssociationDialog.OK)
			assoc = dialog.getResult(); 

		if(assoc==null)
			return false;
		
		/* add a classifier corresponding to the association */
		instanceSpec.getClassifiers().add(assoc);
				
		((Package)(target.getOwner())).getPackagedElements().add(instanceSpec);
		return true;
	}
	
	/* (non-Javadoc)
	 * @see com.cea.papyrus.core.common.factories.GraphEdgeFactory#deconnect(org.eclipse.uml2.uml.Element, org.eclipse.uml2.uml.Element, org.eclipse.uml2.uml.Element)
	 */
	/**
	 * Deconnect.
	 * 
	 * @param target the target
	 * @param link the link
	 * @param source the source
	 * 
	 * @return true, if deconnect
	 */
	@Override
	public boolean deconnect(Element source, Element target, Element link){
		InstanceSpecification instanceSpec=(InstanceSpecification)link;
		instanceSpec.setName(getNewUMLElementName(link.getOwner()));
		instanceSpec.getClassifiers().clear();
		return true;
	}
	
	/* (non-Javadoc)
	 * @see com.cea.papyrus.core.common.factories.IGraphEdgeFactory#canExecute(com.cea.papyrus.diagraminterchange2.di2.GraphNode, com.cea.papyrus.diagraminterchange2.di2.GraphNode)
	 */	
	/**
	 * Can connect.
	 * 
	 * @param target the target
	 * @param source the source
	 * 
	 * @return true, if can connect
	 */
	@Override
	public boolean canConnect(Element source,Element target){
		// if no source or no target found
		if ((source != null) &&(source instanceof InstanceSpecification) &&(target == null)) {
			return true;
		}
		// Test source and target types
		if ((source instanceof InstanceSpecification) && (target instanceof InstanceSpecification))
		{ // they are  instance specifications
			return true;
		}
		return false;
	}
}
